// pages/courseIndex/courseIndex.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
        // 轮播数据
        wheelData: [],
		goodsType: 'all',
		sortPrice: 0,

		chooseMenu: null,
		categories: [],

		page: 1,
		hasMore: true,
		data: [],
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.onPullDownRefresh();
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
        this.loadWheelData();
		this.loadCategories();
		this.loadData(1).finally(() => {
			wx.stopPullDownRefresh();
		});
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		if (!this.data.hasMore) return;

		const page = this.data.page + 1;
		this.loadData(page);
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {
		const image = "";
		return {
			imageUrl: image,
			path: this.route
		}
	},

    onWheelToTap: function (e) {
        const dataset = e.currentTarget.dataset,
            index = dataset.index;
        const item = this.data.wheelData[index];
        if (item.type.value == 10) {
            wx.navigateTo({
                url: '/pages/mall/goods-detail/goods-detail?id=' + item.goods_id,
            });
        } else if (item.type.value == 20) {
            wx.navigateTo({
                url: '/pages/course/detail/detail?id=' + item.goods_id,
            });
        } else if (item.type.value == 30) {
            wx.navigateTo({
                url: '/pages/activity/detail/detail?id=' + item.goods_id,
            });
        }
    },

    // 加载轮播
    loadWheelData: function () {
        return wx.http.get('api/index/wheel', {
            display_type: 20
        }).then((res) => {
            res = res.data.data.getList;
            this.setData({
                wheelData: res,
            });
            return res;
        }, (err) => {
            console.warn("api/index/wheel", err)
        });
    },

	// 加载分类信息
	loadCategories: function() {
		return wx.http.get('api/curriculum.category/index').then((res) => {
			const categories = res.data.data.list;
			this.setData({
				chooseMenu: this.data.chooseMenu,
				categories: categories
			});
			return categories;
		}, (err) => {
			console.warn("api/curriculum.category/index", err)
		});
	},

	// 加载数据
	loadData: function(page = 1) {
		const chooseMenu = this.data.chooseMenu;
		const categoryId = chooseMenu ? chooseMenu.category_id : '';
		return wx.http.get('api/curriculum/lists', {
			page: page,
			sortType: this.data.goodsType,
			sortPrice: this.data.sortPrice,
			search: '',
			course_id: '',
			category_id: categoryId,
		}).then((res) => {
			res = res.data.data.list;
			const hasMore = res.data.length >= res.pre_page;
			const data = page > 1 ? this.data.data.concat(res.data) : res.data;
			this.setData({
				page: page,
				hasMore: hasMore,
				data: data,
			});
			return res.data;
		}, (err) => {
			console.warn("api/curriculum/lists", err)
		});
	},

	// 左侧分类切换
	goodsMenuTap: function(e) {
		const index = e.currentTarget.dataset.index;
		const menu = this.data.categories[index];
		this.data.chooseMenu = menu;
		this.setData({
			chooseMenu: menu
		});
		this.loadData(1);
	},

	// 菜单被点击
	menuItemTap: function(e) {
		const goodsType = e.currentTarget.dataset.state;
		const data = {goodsType: goodsType};

		// 处理价格排序
		if (goodsType === "price" && this.data.goodsType === "price") {
			const sortPrice = this.data.sortPrice ? 0 : 1;
			data.sortPrice = sortPrice;
			this.data.sortPrice = sortPrice;
		}

		this.data.goodsType = goodsType;
		this.setData(data);
		this.loadData(1);
	},
});
